<?php
class gateway implements gatekeeper
{
	/*
		Set Var Permissions
	*/
	protected $Login; // Stores Login Information
	private $Action; // Stores the Scripts Action
	private $Form; // Stores the Form Data
	
	public function __construct()
	{
		// Globals		
		Global $lib, $auth, $mySql;
		$this->lib =& $lib;
		$this->auth =& $auth;
		$this->mySql =& $mySql;
		
		// Get Site Settings
		$this->Settings = site::getSettings();
		
		// Looks for Action Variables
		// further variable are obtained class specifically

		/*
		*	Current In-Use Actions
		*	
		*	- 'register' (Registration)
		*	- 'user' (User Edit)
		*	- 'message' (Message Actions)
		*

		*/	
		$this->setField("action");
		$this->Action =& $this->action;
		if(EMPTY($this->Action))
		{
			$this->Action = "list";
		}			
	}
	
	public function runClass()
	{
		#############################
		#								#
		#		Auth Logic	 				#
		#								#
		#############################
		// Find Login Status
		$this->auth->runClass();
		if($this->auth->getLoginStatus())
		{
			$this->LoginStatus = true;
			$this->Login = $this->auth->getLoginInfo();
		}
		else
		{
			$this->LoginStatus = false;
		}
		
		
		
		#############################
		#								#
		#		Back-End Logic 				#
		#								#
		#############################
		
		/*
		*	Registration Logic
		*/
		
		// Checks if the Action defined is to register
		if("register" == $this->Action)
		{
			$this->registration = new registration;
			$this->registration->runClass();
		}
		
		// Checks if the Action Defined is to change a relation
		elseif("user" == $this->Action)
		{
			$this->setField("section");
			if("follow" == $this->section)
			{
				$this->startClass("relations");
				$this->relations->runClass();
			}
						
		}
		elseif("message" == $this->Action)
		{
		}
		elseif("list" == $this->Action)
		{
		}
		
		#############################
		#								#
		#		Display Logic 				#
		#								#
		#############################
		
		
		// Checks to make sure the Page is not being redirected
		if(EMPTY($this->redirect))
		{
			// Open and Initiate the Navigation Class
			Global $messages;
			require_once(SITE_PATH_COMMON . "nav.class.php");
			$this->nav = new nav;
			$this->nav->setLoginData($this->Login);
			// Build Navigation Bar and Header
			$this->nav->buildNavBar();
			
			// Checks if the Page is related to Register
			if("register" == $this->Action)
			{
				
			}
			
			// Checks to see if the page is related to Writing
			elseif("write" == $this->Action)
			{
				// Display Password Form
			}
			
			// Checks if User is going into Options panel
			elseif("user" == $this->Action)
			{
				$this->nav->buildUserControl();
			}
			// Deafults to the Message Page			
			else
			{	
				if($this->LoginStatus)
				{
					// Builds the Navigation Bar
					$this->nav->buildNavBar();
					
					// Build the Message Box
					$this->nav->buildMessageBox();
					
				}
				else
				{
					$this->nav->buildNavBar();
					echo "<div id=pagecontent><div id=topalert class=headerbottom >Go to <a href=?welcome=1>Welcome Page</a></div></div>";
					require_once("inc/form/form.inc");
					require_once("inc/form/reg.inc");
					require_once("inc/logo.inc");
				}
				
			}
			// Displays any MySql Errors
			$this->mySql->listErrors();
		}
		else
		{
			$this->lib->redirect($this->redirect);
		}
		if($this->Settings["development"])
		{
			//print_r($this->Login);
		}
		
	}
	/** 
	* Set ID fields in order of inbound presedence 
	* 
	* @param string $field 
	*/ 
	private function setField($field, $value = FALSE) 
	{ 
		// Manuall Set Value 
		if (FALSE != @$value) 
		{ 
			$this->$field = $value; 
		} 
		// Form Data First 
		elseif (FALSE != @$_POST["form"][$field]) 
		{ 
			$this->$field = $_POST["form"][$field]; 
		} 
		// General Post data next 
		elseif (FALSE != @$_POST[$field]) 
		{ 
			$this->$field = $_POST[$field]; 
		} 
		// Get Data last 
		elseif (FALSE != @$_GET[$field]) 
		{ 
			$this->$field = $_GET[$field]; 
		} 
		// But it can't be an array 
		if (FALSE != @is_array($this->$field)) 
		{ 
			unset($this->$field); 
		} 
	}
	private function startClass($class)
	{
		Global $$class;
			$this->$class = $$class;
	}
}
?>